# -*- coding: utf-8 -*-
"""
@FileName：file_tool.py
@Description：
@Author：锦沐Python
@Time：2024/8/31 18:43
"""

import csv
import os

from ReLearning.src.config import CSV_DATA_PATH, CSV_LEARN_PATH
print(CSV_DATA_PATH)


def write_example_expression_data(data):
    """
    写入示例表达式
    :param data:
    """
    file_exists = os.path.isfile(CSV_DATA_PATH) and os.path.getsize(CSV_DATA_PATH) == 0
    with open(CSV_DATA_PATH, 'a', newline='', encoding='utf-8') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames= ["type", 'description', 'expression'])
        # 写入标题行
        if file_exists:
            writer.writeheader()
        # 逐行写入数据
        for row in data:
            writer.writerow(row)

def read_example_expression_data():
    """
    读取示例表达式
    """
    with open(CSV_DATA_PATH, 'r', newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        return list(reader)

def read_learn_data():
    """
    读取示例表达式
    """
    with open(CSV_LEARN_PATH, 'r', newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        return list(reader)
